import { IFeidaoAiWeappPage } from '@dfeidao/atom-wxapp/interfaces';
import nodejs from '@dfeidao/atom-wxapp/msg/nodejs';
import render from '@dfeidao/atom-wxapp/render/render';
import local_set from '@dfeidao/atom-wxapp/local/set';
import video_manage from '@dfeidao/atom-wxapp/api/media/create-video-context';

import { file_url, spaceid } from '../atom/config';

export default async function a001(fd: IFeidaoAiWeappPage) {
	// todo
	// const commodity_sale_no = 'CSN000001';
	const commodity_no = fd.data.params.commodity_no;

	const res = await nodejs<{ pic_type: string; bander: Array<{ pic_path: string, preview_pic: string, bstype: string, index: number }>, pictures: Array<{ pic_path: string, img: string, word_text: string }> }>('app-commodity-del/s002', {
		filter: {
			commodity_no
		}
	});
	// const res = {
	// 	"bander": [],
	// 	"pictures": [{ "_id": "8f1e7c23-3cfc-4676-8d50-4fb99a87ba60", "pic_path": "", "word_text": "查看教学班下所有授课教师信息查看教学班下所有授课教师信息查看教学班下所有授课教师信息查看教学班下所有授课教师信息查看教学班下所有授课教师信息查看教学班下所有授课教师信息查看教学班下所有授课教师信息、" }, { "_id": "4e9002e0-0a11-4457-b0c8-53be3de9d50e", "pic_path": "00f7eb64-3951-4997-8e69-15862a371044.gif", "word_text": null }, { "_id": "8f1e7c23-3cfc-4676-8d50-4fb99a87ba60", "pic_path": "", "word_text": "查看教学班下所有授课教师信息、" }, { "_id": "4e9002e0-0a11-4457-b0c8-53be3de9d50e", "pic_path": "00f7eb64-3951-4997-8e69-15862a371044.gif", "word_text": null }, { "_id": "8f1e7c23-3cfc-4676-8d50-4fb99a87ba60", "pic_path": "", "word_text": "查看教学班下所有授课教师信息、" }, { "_id": "4e9002e0-0a11-4457-b0c8-53be3de9d50e", "pic_path": "00f7eb64-3951-4997-8e69-15862a371044.gif", "word_text": null }, { "_id": "8f1e7c23-3cfc-4676-8d50-4fb99a87ba60", "pic_path": "", "word_text": "查看教学班下所有授课教师信息、" }, { "_id": "4e9002e0-0a11-4457-b0c8-53be3de9d50e", "pic_path": "00f7eb64-3951-4997-8e69-15862a371044.gif", "word_text": null }]
	// };
	const _bander = [] as string[];
	const bander = res.bander.map((i) => {
		_bander.push(file_url + 'getfile?productid=' + spaceid + '&id=' + i.pic_path);
		return { ...i, pic_path: file_url + 'getfile?productid=' + spaceid + '&id=' + i.pic_path, preview_pic: file_url + 'getfile?productid=' + spaceid + '&id=' + i.preview_pic };
	});
	const p = [] as Array<{ url: string; gif: boolean; word_text: string; }>;
	const pictures = res.pictures.filter((item) => {
		const url = file_url + 'getfile?productid=' + spaceid + '&id=' + item.pic_path;
		p.push({ url, gif: /\.gif/.test(url), word_text: item.word_text });
		return item.pic_path && !/\.gif/.test(item.pic_path);
	}).map((i) => {
		return file_url + 'getfile?productid=' + spaceid + '&id=' + i.pic_path;
	});
	local_set(fd, 'pictures', pictures);

	local_set(fd, 'bander', _bander);


	const videoManage = bander.filter((item, i) => {
		return item.bstype === 'video';
	});
	const arr = new Array();
	for (let i = 0; i < videoManage.length; i++) {
		const id = 'video' + videoManage[i].index;
		arr.push({
			index: videoManage[i].index,
			manage: await video_manage(id)
		});
	}
	local_set(fd, 'current', 0);
	local_set(fd, 'videoManage', arr);

	render(fd, {
		bander,
		show: false,
		pic_type: res.pic_type,
		pictures: p,
		flag4: 0,
		autoplay: true,
		current: 0,
		interval: 5000,
		duration: 500,
		circular: true,
		controls: false,	// 显示控制
		show_center_play_btn: true, // 显示中间的播放按钮
		showDots: true    // 显示原点
	});


}
